package com.example.demo.service.impl;

import com.example.demo.entity.Coalexceptionfile;
import com.example.demo.handle.ExxceptionFileListParseHandler;
import com.example.demo.mapper.CoalexceptionfileMapper;
import com.example.demo.service.CoalexceptionfileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-28
 */
@Service
public class CoalexceptionfileServiceImpl extends ServiceImpl<CoalexceptionfileMapper, Coalexceptionfile> implements CoalexceptionfileService {

    @Autowired
    private CoalexceptionfileMapper coalexceptionfileMapper;

    @Override
    public Integer insertExceptionData(String ExceptionXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            ExxceptionFileListParseHandler exxceptionFileListParseHandler = new ExxceptionFileListParseHandler();
            try {
                // 读取xml文件
                parser.parse(ExceptionXml, exxceptionFileListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + exxceptionFileListParseHandler.getCoalexceptionfileArrayList().size() + "个节点被存储");
            for (Coalexceptionfile coalexceptionfile : exxceptionFileListParseHandler.getCoalexceptionfileArrayList()) {
                System.out.println(coalexceptionfile.getCsMineCode());
                System.out.println(coalexceptionfile.getCsDataTime());
                System.out.println(coalexceptionfile.getOmWeighterCode());
                System.out.println(coalexceptionfile.getOmOromoteMode());
                System.out.println(coalexceptionfile.getOmAlarmState());
                System.out.println(coalexceptionfile.getOmAlarmStime());
                System.out.println(coalexceptionfile.getOmAlarmEtime());
                System.out.println("----finish----");
                coalexceptionfileMapper.insertExceptionData(coalexceptionfile.getCsMineCode(),coalexceptionfile.getCsDataTime(),coalexceptionfile.getOmWeighterCode(),coalexceptionfile.getOmOromoteMode(),coalexceptionfile.getOmAlarmState(),coalexceptionfile.getOmAlarmStime(),coalexceptionfile.getOmAlarmEtime());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
